package com.integral.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.integral.entity.UserInfo;

public class Interceptor implements HandlerInterceptor {

	/**
	 * 请求处理之前调用方法 1
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
		if(null == userInfo){
			response.sendRedirect(request.getContextPath()+"/login.do");
			return false;
		}
		
		//System.out.println("aaa:"+request.getContextPath());
		return true;
	}

	/**
	 * 请求处理之后，页面渲染之前调用方法 2
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 请求处理之后，页面渲染之后调用方法 3
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
